﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LisaCore;
using ActionReporter;
using InputHandler;

namespace LocalLisaTestGUI
{
    public partial class mainForm : Form
    {
        private ICore lisaCore;
        private UDPClient userCommandSender;
        private UDPClient environmentEventSender;

        public mainForm()
        {
            InitializeComponent();
            IActionReporter actionReporter = new ActionReporterImpl();
            actionReporter.CommandExecuted += new HandleAction(actionReporter_CommandExecuted);
            actionReporter.StateChanged += new HandleAction(actionReporter_StateChanged);
            actionReporter.UserPermissionRequired += new HandleAction(actionReporter_UserPermissionRequired);
            actionReporter.InfoUpdate += new HandleAction(actionReporter_InfoUpdate);
            lisaCore = new CoreUnit();
            lisaCore.initialize();
            lisaCore.ActionReporter = actionReporter;
            userCommandSender = new UDPClient(127001,6060,127001,6767);
            environmentEventSender = new UDPClient(127001, 6061,127001,6768);
            lisaCore.start();
        }

        #region ActionReporter event handling methods
        void actionReporter_UserPermissionRequired(ActionEventArgs args)
        {
            updateLisaReportGrid("Permission: ", args.Action);
        }

        void actionReporter_StateChanged(ActionEventArgs args)
        {
            updateLisaReportGrid("State Change: ", args.Action);
        }

        void actionReporter_CommandExecuted(ActionEventArgs args)
        {
            updateLisaReportGrid("Command: ", args.Action);
        }

        void actionReporter_InfoUpdate(ActionEventArgs args)
        {
            updateLisaReportGrid("Info: ", args.Action);
        }
        #endregion

        #region GUI Event methods
        private void sendCommand_Click(object sender, EventArgs e)
        {
            userCommandSender.sendData(argumentBox.Text);
        }

        private void sendEvent_Click(object sender, EventArgs e)
        {
            environmentEventSender.sendData(argumentBox.Text);
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            lisaCore.stop();
        }
        #endregion

        #region Private helper methods
        void updateLisaReportGrid(String type, String action)
        {
            String[] newVals = { type, action };
            lisaReportGrid.Rows.Add(newVals);
        }
        #endregion
    }
}
